package com.zhangxiaofanfan.hotcode.fourth.third;

import java.util.Arrays;

/**
 * <a href="https://leetcode.cn/problems/daily-temperatures/description/?envType=study-plan-v2&envId=top-100-liked">739. 每日温度</a>
 *
 * @date 2024-07-13 03:14:13
 * @author zhangxiaofanfan
 */
public class HotCode072 {
    public static void main(String[] args) {
        HotCode072 hotCode = new HotCode072();
        int[] temperatures = {73,74,75,71,69,72,76,73};
        System.out.println(Arrays.toString(hotCode.dailyTemperatures(temperatures)));
    }

    public int[] dailyTemperatures(int[] temperatures) {
        int[] result = new int[temperatures.length];
        for (int i = temperatures.length - 2, step; i >= 0; i--) {
            step = 1;
            while (true) {
                if (temperatures[i + step] > temperatures[i]) {
                    result[i] = step;
                    break;
                } else if (result[i + step] == 0) {
                    result[i] = 0;
                    break;
                } else {
                    step += result[i + step];
                }
            }
        }
        return result;
    }
}
